package common_phrase

import (
	"errors"
	"hft/Database/admin/common_phrase"
	"hft/e"
	"hft/global"
)

type CommonPhrase struct{}

func (*CommonPhrase) List(category_id int) []*common_phrase.CommonPhrase {
	var list []*common_phrase.CommonPhrase
	global.OC_DB.Where("category_id = ?", category_id).Find(&list)
	return list
}

func (*CommonPhrase) Add(data *common_phrase.CommonPhrase) (err error) {
	if err = global.OC_DB.Create(&data).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

func (*CommonPhrase) Edit(data *common_phrase.CommonPhrase) (err error) {
	if res := global.OC_DB.Model(&common_phrase.CommonPhrase{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"chat_text": data.ChatText,
	}); res.RowsAffected == 0 || res.Error != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

func (*CommonPhrase) Delete(ids []int) (err error) {
	if err = global.OC_DB.Unscoped().Delete(&common_phrase.CommonPhrase{}, ids).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

var CommonPhraseService = new(CommonPhrase)
